<template>
  <el-container>
    <el-main>
      <el-row>
        <el-col :span="22" :offset="1">
          <el-button @click="addhostdialog" size="mini">添加从机</el-button>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="22" :offset="1">
          <el-table :data="tableData" border size="mini">
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="hostaddr" label="主机地址"></el-table-column>
              <el-table-column prop="port" label="主机端口"></el-table-column>
              <el-table-column prop="protocol" label="协议"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="text" @click="Connect(scope.row)">连接</el-button>
                  <el-button type="text" @click="EditHost(scope.row.id)">修改</el-button>
                  <el-button type="text" @click="DeleteHost(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
        </el-col>
      </el-row>

    </el-main>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      hostId: "",
      tableData: [],
      title: false,
    };
  },
  created() {
    this.gethosts();
  },
  methods: {
    Connect(row) {
      let url = row.protocol + "://" + row.hostaddr + ":" + row.port;
      this.$http.Put(
        this.$api.connecthost + "/" + row.id,
        {},
        (res) => {
          this.$cookies.set("URL", url);
          this.$cookies.set("RTOKEN", res);
          this.$cookies.set("SERVERNAME", row.name);
          this.$store.commit("settings/changeValue", row.name);
        }
      );
    },
    EditHost(id) {
      this.$router.push({
        path:"/hosts/addhost",
         query:{
          id: id
        }
      })
    },
    gethosts() {
      this.$http.Get(this.$api.hosts, {}, (res) => {
        this.tableData = res;
      });
    },
    addhostdialog() {
      this.$router.push({
        path:"/hosts/addhost",
      })
    },
    DeleteHost(id) {
      this.$http.Delete(this.$api.host + "/" + id, {}, () => {
        this.gethosts();
      });
    },
  },
};
</script>